﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace PJ.Data
{
    /// <summary>
    /// EntityPICProvider : Persist In Cache data provider
    /// </summary>
    /// <typeparam name="I">Object to provide interface type</typeparam>
    /// <typeparam name="C">Object to provide concrete class type</typeparam>
    public abstract class EntityPICProvider<I, C> : ObjectPICProvider<I, C>, IEntityProvider<I>
        where C : BaseEntityData, I, new()
        where I : class, IEntityData
    {
        public EntityPICProvider(ICacheManager cache) : base(cache)
        {
        }

        public I GetById(string id)
        {
            if (id == null)
                throw new ArgumentNullException();
            return (from d in DataObjects where d.Id == id select d).Single();
        }

        public I FindById(string id)
        {
            if (id == null)
                throw new ArgumentNullException();
            return (from d in DataObjects where d.Id == id select d).SingleOrDefault();
        }

        public I CachedGetById(string id)
        {
            return GetById(id);
        }

        public I CachedFindById(string id)
        {
            return FindById(id);
        }

        #region Interface override
        IEntityData IEntityProvider.GetById(string id)
        {
            return GetById(id);
        }

        IEntityData IEntityProvider.CachedGetById(string id)
        {
            return CachedGetById(id);
        }

        IEntityData IEntityProvider.FindById(string id)
        {
            return FindById(id);
        }

        IEntityData IEntityProvider.CachedFindById(string id)
        {
            return CachedFindById(id);
        }

        #endregion

    }
}
